package com.kingwood.algorithm.test200;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

// 字符串拼接
// https://fcqian.blog.csdn.net/article/details/135232122
public class ZiFuChuanPinJie {

    static int k;
    static int len;
    static HashSet<String> resSet = new HashSet<>();

    public static int getResult(String str, int k) {
        if (str.length() < k) {
            return 0;
        }

        char[] charArr = str.toCharArray();
        for (char c : charArr) {
            if (c < 'a' || c > 'z') {
                return 0;
            }
        }

        Arrays.sort(charArr);
        len = charArr.length;

        dfs(charArr, -1, 0, new boolean[len], new StringBuilder());
        return resSet.size();
    }

    public static void dfs(char[] charArr, int pre, int level, boolean[] used, StringBuilder sb) {
        if (level == k) {
            resSet.add(sb.toString());
            return;
        }

        for (int i=0; i<len; i++) {
            if (used[i]) {
                continue;
            }

            char cur = charArr[i];
            // 相同的字符不能相邻
            if (pre >= 0 && charArr[pre] == charArr[i]) {
                continue;
            }

            used[i] = true;
            sb.append(cur);
            dfs(charArr, i, level+1, used, sb);
            used[i] = false;
            sb.deleteCharAt(sb.length() - 1);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] strArr = scanner.nextLine().split(" ");
        String str = strArr[0];
        k = Integer.parseInt(strArr[1]);
        int ans = getResult(str, k);
        System.out.println(ans);

        Iterator<String> iterator = resSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
